package com.becca.cake.core.task.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.becca.cake.core.constant.CommonConstants;
import com.becca.cake.core.sms.SMSHelper;
import com.becca.cake.core.sms.message.SmsUserCoupon;
import com.becca.cake.core.task.Task;
import com.becca.cake.dao.users.UserManagerDAO;
import com.becca.cake.model.users.UserCouponModel;

public class UserCouponTask implements Task {
    private static final Logger logger    = LoggerFactory.getLogger(UserCouponTask.class);

    private UserManagerDAO      userManagerDao;

    private boolean             executing = false;

    @Override
    public void doTask() {
        if (executing) {
            return;
        }
        executing = true;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userCouponDay", CommonConstants.YUNPIAN_USER_COUPON_DAY);
        List<UserCouponModel> userCouponList = this.userManagerDao
            .selectAllUsableUserCouponModel(map);
        //过期时间
        String userEndDate = CommonConstants.YUNPIAN_USER_COUPON_DAY + "天后";
        if (userCouponList != null && userCouponList.size() > 0) {
            for (UserCouponModel userCouponModel : userCouponList) {
                String userMobileNo = userCouponModel.getUserMobileNo();
                String couponNumber = userCouponModel.getCouponNumber();
                if (StringUtils.isNotEmpty(userMobileNo)) {
                    SmsUserCoupon sms = new SmsUserCoupon(userMobileNo, couponNumber, userEndDate);
                    if (SMSHelper.sendMessageMass(sms)) {
                        logger.info("信息发送成功！");
                    }
                }
            }
        }
        executing = false;
    }

    public UserManagerDAO getUserManagerDao() {
        return userManagerDao;
    }

    public void setUserManagerDao(UserManagerDAO userManagerDao) {
        this.userManagerDao = userManagerDao;
    }

}
